package com.hfdg.process.ctrl;

import com.github.pagehelper.PageInfo;
import com.hfdg.process.service.ProcessDataService;
import com.hfdg.prototype.base.model.AjaxResult;
import com.hfdg.prototype.base.service.CommonsDataService;
import org.apache.catalina.User;
import org.apache.commons.collections.CollectionUtils;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.Process;
import org.flowable.bpmn.model.UserTask;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.DeploymentBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

/**
 * @ClassName ProcessSettingCtrl
 * @Description TODO
 * @Author yuelh
 * @Date 2020-1-28 22:01
 * @Version 1.0
 * @Company 会飞的锅工作室
 **/
@Controller
@RequestMapping("/processSetting")
public class ProcessSettingCtrl {
    @Autowired
    RepositoryService repositoryService;
    @Autowired
    CommonsDataService commonsDataService;
    @Autowired
    ProcessDataService processDataService;



    @RequestMapping("/deploy")
    @ResponseBody
    public AjaxResult deploy(MultipartFile bpmnFile,MultipartFile formFile,String processName,String category,String description){
        try {
            DeploymentBuilder deploymentBuilder = repositoryService.createDeployment().addInputStream(bpmnFile.getOriginalFilename(),bpmnFile.getInputStream());
            deploymentBuilder.name(processName);
            deploymentBuilder.category(category);
            Deployment deployment = deploymentBuilder.deploy();

            return AjaxResult.success();
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error(-1,e.getMessage());
        }
    }
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(String deploymentId){
        repositoryService.deleteDeployment(deploymentId,true);
        return AjaxResult.success();
    }
    @RequestMapping("/to/processDefinitionListView")
    public String toProcessDefinitionListView(String category){
        return "commons/process/setting/processDefinitionListView";
    }

    @RequestMapping("/getPagingProcessDefinition")
    @ResponseBody
    public AjaxResult getPagingProcessDefinition(int offset, int limit, String orderBy, Map<String, Object> params){
        return AjaxResult.success(processDataService.getPagingProcessDefinition(offset,limit,orderBy,params));
    }
    @RequestMapping("/view/{viewName}")
    public String view(@PathVariable String viewName){
        return "setting/process/"+viewName;
    }

    @RequestMapping("/to/taskHandlerSetting")
    public String listTaskNode(String processDefinitionId, Model model){
        Process process =  repositoryService.getBpmnModel(processDefinitionId).getProcesses().get(0);
        List<UserTask> userTaskList = new ArrayList<>();
        Collection<FlowElement> flowElements = process.getFlowElements();
        if (CollectionUtils.isNotEmpty(flowElements)) {
            for (FlowElement flowElement : flowElements) {
                if (flowElement instanceof UserTask) {
                    userTaskList.add((UserTask) flowElement);
                }
            }
        }
        model.addAttribute("userTaskList",userTaskList);
        return "commons/process/setting/taskHandlerSetting";
    }



}
